package com.qtummatrix.utils;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * create by Gongshiyong  2019-12-07 11:02
 */
public class getExcelUtil {
    public static List<String[]> getExcel(String filePath) {
        // 创建输入流
        List<String[]> list = new ArrayList<String[]>();
        // 创建输入流
        InputStream stream;
        try {
            stream = new FileInputStream(filePath);
            // 获取Excel文件对象
            Workbook rwb;
            try {
                rwb = Workbook.getWorkbook(stream);
                // 获取文件的指定工作表 默认的第一个
                Sheet sheet = rwb.getSheet(0);
                // 行数(表头的目录不需要，从1开始)
                for (int r = 1; r < sheet.getRows(); r++) {
                    // 创建一个数组 用来存储每一列的值
                    String[] str = new String[sheet.getColumns()];
                    Cell cell = null;
                    // 列数
                    for (int j = 0; j < sheet.getColumns(); j++) {
                        // 获取第i行，第j列的值
                        cell = sheet.getCell(j, r);
                        str[j] = cell.getContents();
                    }
                    // 把刚获取的列存入list
                    list.add(str);

                }
                return list;
            } catch (BiffException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
